import Vue from 'vue'
import VueRouter, { RouteConfig, Route } from 'vue-router'
import login from '../components/login.vue'
import Home from '../components/Home.vue'
Vue.use(VueRouter)
const routes: Array<RouteConfig> = [
  {
    path: '/',
    redirect: 'login'
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/Home',
    name: 'Home',
    component: Home
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to: Route, _: Route, next: any) => {
  const tokenStr = window.sessionStorage.getItem('token')
  if (to.path === '/login') return next()
  if (!tokenStr) return next('/login')
  next()
})
export default router
